package com.example.s100_museum.Controller;


import com.example.s100_museum.pojo.*;
import com.example.s100_museum.service.impl.TTicketDataServiceImpl;
import com.example.s100_museum.service.impl.TTicketServiceImpl;
import com.example.s100_museum.service.impl.TTicketSumServiceImpl;
import com.example.s100_museum.service.impl.TUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin("*")
public class DemoController {

    @Autowired
    TUserServiceImpl userdao;
    /**
     * 登录
     */
    @RequestMapping("/login")
    public TUser login(TUser tUser){
        System.out.println("tUser = " + tUser);

        try {
            List<TUser> list = userdao.lambdaQuery().eq(TUser::getName, tUser.getUsername()).eq(TUser::getPassword, tUser.getPassword()).list();

            if(list.isEmpty()) {
                return null;
            }
            return list.get(0);
        }catch (Exception e){
            return null;

        }


    }


    /**
     * 注册账号
     */
    @RequestMapping("/add_login")
    public String addLogin(TUser tUser){

        System.out.println("tUser = " + tUser);
        tUser.setId(null);


        try {
            boolean save = userdao.save(tUser);
            if(save) {
                return "注册账号成功";
            }
            return "注册账号失败 可能已经存在当前账号";
        }catch (Exception e){
            return "注册账号失败 可能已经存在当前账号";
        }
    }




    @Autowired
    TTicketServiceImpl ticketDao;

    @Autowired
    TTicketDataServiceImpl dataDao;


    @Autowired
    TTicketSumServiceImpl dsumDao;


    /**
     * 下单
     */
    @RequestMapping("/add_pay")
    public String addPay(TTicket ticket){
//        ticket.setId(null);
        String random = "pay-" + UUID.randomUUID();
        ticket.setOrderNumber(random);
        System.out.println("tUser = " + ticket);

        try {
            boolean save = ticketDao.save(ticket);

            if(save) {
                dataDao.save(new TTicketData().setData(new Date().toString()).setOrderNumber(random));
                dsumDao.save(new TTicketSum().setOrderNumber(random).setTotalPrices((100*Integer.parseInt(ticket.getQuantity()))+""));
                return "下单成功";
            }
            return "下单失败";
        }catch (Exception e){
            return "下单失败"+e.getMessage();
        }
    }

    @RequestMapping("/get_pay")
    public List<TicketModel> getTicket(String username) {
        return ticketDao.getTicket(username);
    }


}
